package edu.chen.service.impl;

import edu.chen.bean.Product;
import edu.chen.dao.impl.ProductDaoImplByJdbc;
import edu.chen.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productServiceImplByJdbc")
public class ProductServiceImplByJdbc implements ProductService{
    private ProductDaoImplByJdbc productDaoImplByJdbc;

    @Autowired
    public void setProductDaoImplByJdbc(ProductDaoImplByJdbc productDaoImplByJdbc) {
        this.productDaoImplByJdbc = productDaoImplByJdbc;
    }

    public boolean exist(int id) {
        if(0 != productDaoImplByJdbc.query(id).getId()){
            return true;
        }
        return false;
    }

    public boolean exist(String name) {
        if (0 != productDaoImplByJdbc.query(name).size()){
            return true;
        }
        return false;
    }

    public int save(Product product) {
        return productDaoImplByJdbc.insert(product);
    }

    public boolean delete(int id) {
        if(productDaoImplByJdbc.delete(id) == 1)
            return true;
        return false;
    }

    public Product query(int id) {
        return productDaoImplByJdbc.query(id);
    }


    public List<Product> query(String name) {
        return productDaoImplByJdbc.query(name);
    }
}
